﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.IO;
using System.Xml;

namespace DusonFramework.Core.Ping
{
    public class PingService
    {
        private static void Execute(string service, Uri url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(service);
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.Timeout = 3000;
                request.Credentials = CredentialCache.DefaultNetworkCredentials;

                AddXmlToRequest(request, url);
                request.GetResponse();
            }
            catch (Exception)
            {
                // Log the error.
            }
        }

        /// <summary>
        /// Adds the XML to web request. The XML is the standard
        /// XML used by RPC-XML requests.
        /// </summary>
        private static void AddXmlToRequest(HttpWebRequest request, Uri url)
        {
            Stream stream = (Stream)request.GetRequestStream();
            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("methodCall");
                writer.WriteElementString("methodName", "weblogUpdates.ping");
                writer.WriteStartElement("params");
                writer.WriteStartElement("param");
                writer.WriteElementString("value", "value");
                writer.WriteEndElement();
                writer.WriteStartElement("param");
                writer.WriteElementString("value", url.ToString());
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
        }
    }
}
